package com.yangwp.java.spider.weibo.service.impl;

import javax.jws.soap.SOAPBinding;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import com.yangwp.java.spider.weibo.dao.UserDao;
import com.yangwp.java.spider.weibo.model.User;
import com.yangwp.java.spider.weibo.service.UserService;


/**
 * @author 11070538
 */

@Service("UserService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public boolean save(User user){
        return userDao.insert(user) > 0;
    }

    @Override
    public boolean save(List<User> users){
        if(users == null || users.size() == 0){
            return false;
        }
        int num = 0;
        try{
            num = userDao.batchInsert(users);
        }catch (DuplicateKeyException e){
            System.out.println("HtmlService.save error: duplicate key;" + e.getMessage());
        }catch (Exception e){
            System.out.println("HtmlService.save error:" + e.getMessage());
        }
        System.out.println("HtmlService.save:插入" + num + "/" + users.size());
        return num > 0;
    }

    @Override
    public boolean setTracked(String id){
        if(id == null){
            return false;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(new Date());
        User user = new User();
        user.setUserId(id);
        user.setTrackTime(time);
        return userDao.updateByUserId(user) > 0;
    }

    @Override
    public String getUntracked(){
        User user = userDao.getUntracked();
        return user.getUserId();
    }
}
